<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class orderController extends Controller
{
  

    // 用户列表页
    // admin/order/index
    public function getIndex(Request $request)
    {
        // dd('index');
    	// 读取数据 并且分页
    	 $orders=DB::table('orders')
         ->where(function($query)use($request){
            $query->where('user_id','like','%'.$request->input('keywords').'%');
        })
        ->paginate($request->input('num',10));
        
        //解析模板
        return view('order.index',['orders'=>$orders,'request'=>$request->all()]);  
    }
    
    /**
     * 用户数据的修改 显示表单
     */
    //   /admin/order/edit
    public function getEdit($id)
    {
        // 查询数据
        $res =  DB::table('orders')->where('id',$id)->first();
        //显示模板 分配变量
        return view('order.edit',['orderinfo'=>$res]);
    }

    /**
     * 用户数据的修改 修改数据库
     */
    public function postUpdate(Request $request)
    {
        //接收数据
        $data=$request->only(['status']);
        $id=$request->input('id');
        //执行修改
        $res=DB::table('orders')->where('id',$id)->update($data);
        if($res){
            //成功 页面跳转到列表页
            return redirect('admin/order/index')->with('success','数据修改成功');
        }else{
            //失败 跳转到添加页
            return back()->with('error','数据修改失败');
        }
    }

    public function getXiang($id)
    {   
        $res=DB::table('orders')->where('id',$id)->first();
        $data = [];
        $tmp['id'] = $res->id;
        // dd($data['id']);
        $tmp['address_id'] = $res->address_id;

        $der = DB::table('order_goods')->where('order_id',$tmp['id'])->get();
        // dd($der);
        foreach ($der as $k=>$v)
        {
            $tmp['goods_id'] = $v->goods_id;
            $der2 = DB::table('goods')->where('id',$tmp['goods_id'])->first();
            $tmp['num'] = $v->num;
            $tmp['proname'] = $der2->proname;
            $tmp['price'] = $der2->price;
            $tmp['xiaoji'] = $tmp['price']*$tmp['num'];
            $der1 = DB::table('addresses')->where('id',$tmp['address_id'])->first();
            $tmp['total'] = $res->total;
            $tmp['phone'] = $der1->phone;
            $data[] = $tmp;
        }
        // dd($data1);
        // dd($tmp);
        
        // $data[]=$tmp;
        // dd($data);
        return view('order.xiang',['data'=>$data]);
    }
}
